package p.ithorns.sample.influxdb.core;

import com.influxdb.utils.Arguments;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * InfluxQueryResult
 *
 * @author HtL
 * @date 2025/1/16 14:01
 * @since 1.0.0
 */
public class InfluxQueryResult<T> {

    private final List<Series<T>> series;

    public InfluxQueryResult(@Nonnull List<Series<T>> series) {
        Arguments.checkNotNull(series, "series");
        this.series = series;
    }

    /**
     * Represents one series  of an InfluxQL query.
     */
    public static final class Series<T> {
        @Nonnull
        private final String name;

        @Nonnull
        private final Map<String, String> tags;

        @Nonnull
        private final Map<String, Integer> columns;

        private final List<T> values;

        public Series(final @Nonnull String name, final @Nonnull Map<String, Integer> columns) {
            this(name, new HashMap<>(), columns);
        }

        public Series(final @Nonnull String name,
                      final @Nonnull Map<String, String> tags,
                      final @Nonnull Map<String, Integer> columns) {
            Arguments.checkNotNull(name, "name");
            Arguments.checkNotNull(tags, "tags");
            Arguments.checkNotNull(columns, "columns");

            this.name = name;
            this.tags = tags;
            this.columns = columns;
            this.values = new ArrayList<>();
        }

        /**
         * @return the name
         */
        @Nonnull
        public String getName() {
            return this.name;
        }

        /**
         * @return the tags
         */
        @Nonnull
        public Map<String, String> getTags() {
            return this.tags;
        }

        /**
         * @return the columns
         */
        @Nonnull
        public Map<String, Integer> getColumns() {
            return this.columns;
        }


        /**
         * @return the values
         */
        @Nonnull
        public List<T> getValues() {
            return this.values;
        }

        public void addRecord(@Nonnull final T record) {
            Arguments.checkNotNull(record, "record");
            this.values.add(record);
        }

        @Override
        public String toString() {
            return "{" +
                    "name:'" + name + '\'' +
                    ", tags:" + tags +
                    ", columns:" + columns +
                    ", values:" + values +
                    "}";
        }
    }

    @Override
    public String toString() {
        return "{" +
                "series:" + series +
                "}";
    }
}